﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Eniux.Windows.ThreeDEngine.Helper;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{

    /// <summary>
    ///屏幕空间点元素
    /// </summary>
    public class PointsVisual3D : ScreenSpaceVisual3D
    {
        #region 变量

        /// <summary>
        /// 几何对象点生成器
        /// </summary>
        private readonly PointGeometryBuilder builder;

        #endregion

        #region 注册属性

        #region Size
        /// <summary>
        /// 点大小属性
        /// </summary>
        public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
            "Size", typeof(double), typeof(PointsVisual3D), new UIPropertyMetadata(1.0, GeometryChanged));

        /// <summary>
        /// 获取或设置点大小
        /// </summary>
        public double Size
        {
            get { return (double)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        } 
        #endregion

        #endregion


        #region 构造、析构

        /// <summary>
        /// 默认构造
        /// </summary>
        public PointsVisual3D()
        {
            this.builder = new PointGeometryBuilder(this);
        }

        #endregion


        #region 方法

        /// <summary>
        /// 更新几何对象
        /// </summary>
        protected override void UpdateGeometry()
        {
            this.mesh.Positions = null;
            int n = this.Points.Count;
            if (this.mesh.TriangleIndices.Count != n * 6)
            {
                this.mesh.TriangleIndices = this.builder.CreateIndices(n);
            }

            this.mesh.Positions = this.builder.CreatePositions(this.Points, this.Size, this.DepthOffset);
        }

        /// <summary>
        /// 更新空间转换信息
        /// </summary>
        /// <returns></returns>
        protected override bool UpdateTransforms()
        {
            return this.builder.UpdateTransforms();
        }

        #endregion
    }
}
